from django.shortcuts import render, redirect
from django.core.context_processors import csrf
from django.db import connection, transaction


from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView

# Create your views here.
from rates.models import Rate
from hotels.models import Hotel
from hotels.models import HotelPics


from rates.forms import RateForm



def ratehotel(request, hotel_id=''):

	if 'user_id' not in request.session:
		return redirect('/users/login/')


	rate_form = RateForm()

	hotel_pics = HotelPics.objects.filter(hotel_id=hotel_id)

	hotel_rates = Rate.objects.filter(hotel_id=hotel_id)

	user_id = request.session['user_id']

	context = {
		'hotel_pics': hotel_pics,
		'hotel_rates': hotel_rates,
		'hotel_id': hotel_id,
		'rate_form': rate_form,
		'user_id': user_id,
		}

	context.update(csrf(request))
	return render(request, 'hotels/ratehotel.html', context)


def addrate(request):

	if 'user_id' not in request.session:
		return redirect('/users/login/')

	cursor = connection.cursor()

	user_id = request.session['user_id']
	hotel_id = request.POST.get('hotel_id', '')
	title = request.POST.get('title', '')
	body = request.POST.get('body', '')
	rate = request.POST.get('rate', '')

	insert_flag = None
	insert_flag = cursor.execute("INSERT INTO rates_rate(user_id_id, hotel_id_id, title, body, rate)VALUE(%d, %d, '%s', '%s', '%s')"%(int(user_id), int(hotel_id), str(title), str(body), str(rate)))

	if insert_flag is None:
		return redirect("/rates/ratehotel/%d/"%(int(hotel_id)))
	else:
		return redirect("/rates/ratehotel/%d/"%(int(hotel_id)))
	

